package com.opensource.svgaplayer.utils;

import ohos.agp.components.Image;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * ex.
 *
 * @author wl
 * @since 2021-06-28
 */
public class SVGAScaleInfo {
    private float tranFx = 0.0f;
    private float tranFy = 0.0f;
    private float scaleFx = 1.0f;
    private float scaleFy = 1.0f;
    private float ratio = 1.0f;
    private boolean ratioX = false;

    /**
     * getTranFx
     *
     * @return float
     */
    public float getTranFx() {
        return tranFx;
    }

    /**
     * setTranFx
     *
     * @param  tranFx
     */
    public void setTranFx(float tranFx) {
        this.tranFx = tranFx;
    }

    /**
     * getTranFy
     *
     * @return float
     */
    public float getTranFy() {
        return tranFy;
    }

    /**
     * setTranFy
     *
     * @param tranFy float
     */
    public void setTranFy(float tranFy) {
        this.tranFy = tranFy;
    }

    /**
     * getScaleFx
     *
     * @return float
     */
    public float getScaleFx() {
        return scaleFx;
    }

    /**
     * setScaleFx
     *
     * @param scaleFx float
     */
    public void setScaleFx(float scaleFx) {
        this.scaleFx = scaleFx;
    }

    /**
     * getScaleFy
     *
     * @return float
     */
    public float getScaleFy() {
        return scaleFy;
    }

    /**
     * setScaleFy
     *
     * @param scaleFy float
     */
    public void setScaleFy(float scaleFy) {
        this.scaleFy = scaleFy;
    }

    /**
     * getRatio
     *
     * @return float
     */
    public float getRatio() {
        return ratio;
    }

    /**
     * setRatio
     *
     * @param ratio float
     */
    public void setRatio(float ratio) {
        this.ratio = ratio;
    }

    /**
     * isRatioX
     *
     * @return boolean
     */
    public boolean isRatioX() {
        return ratioX;
    }

    /**
     * setRatioX
     *
     * @param ratioX boolean
     */
    public void setRatioX(boolean ratioX) {
        this.ratioX = ratioX;
    }

    private void resetVar() {
        tranFx = 0.0f;
        tranFy = 0.0f;
        scaleFx = 1.0f;
        scaleFy = 1.0f;
        ratio = 1.0f;
        ratioX = false;
    }

    /**
     * performScaleType
     *
     * @param canvasWidth  float
     * @param canvasHeight float
     * @param videoWidth   float
     * @param videoHeight  float
     * @param scaleType    Image.ScaleMode
     */
    public void performScaleType(float canvasWidth, float canvasHeight, float videoWidth, float videoHeight, Image.ScaleMode scaleType) {
        if (canvasWidth == 0.0F || canvasHeight == 0.0F || videoWidth == 0.0F || videoHeight == 0.0F) {
            return;
        }
        resetVar();
        BigDecimal bgCanvasWidth = new BigDecimal(canvasWidth).subtract(new BigDecimal(videoWidth));
        BigDecimal bgCanvasHeight = new BigDecimal(canvasHeight).subtract(new BigDecimal(videoHeight));
        float canW_vidW_f =bgCanvasWidth.divide(BigDecimal.valueOf(2.0F)).floatValue() ;//(canvasWidth - videoWidth) / 2.0F;
        float canH_vidH_f = bgCanvasHeight.divide(BigDecimal.valueOf(2.0F)).floatValue();//(canvasHeight - videoHeight) / 2.0F;
        float videoRatio = videoWidth / videoHeight;
        float canvasRatio = canvasWidth / canvasHeight;
        float canH_d_vidH = canvasHeight / videoHeight;
        float canW_d_vidW = canvasWidth / videoWidth;
        BigDecimal bigDecimal = null;
        BigDecimal bigDecimal1 = null;
        switch (scaleType) {
            case CENTER:
                tranFx = canW_vidW_f;
                tranFy = canH_vidH_f;
                break;
            case STRETCH:
                ratio = Math.max(canW_d_vidW, canH_d_vidH);
                ratioX = canW_d_vidW > canH_d_vidH;
                scaleFx = canW_d_vidW;
                scaleFy = canH_d_vidH;
                break;
            case ZOOM_START:
                if (videoRatio > canvasRatio) {
                    ratio = canW_d_vidW;
                    ratioX = true;
                    scaleFx = canW_d_vidW;
                    scaleFy = canW_d_vidW;
                } else {
                    ratio = canH_d_vidH;
                    ratioX = false;
                    scaleFx = canH_d_vidH;
                    scaleFy = canH_d_vidH;
                }
                break;
            case ZOOM_CENTER:
                if (videoRatio > canvasRatio) {
                    ratio = canW_d_vidW;
                    ratioX = true;
                    scaleFx = canW_d_vidW;
                    scaleFy = canW_d_vidW;
                    bigDecimal = new BigDecimal(canW_d_vidW).multiply(new BigDecimal(videoHeight));
                    bigDecimal1 = new BigDecimal(canvasHeight).subtract(bigDecimal).divide(BigDecimal.valueOf(2.0F), 2,RoundingMode.HALF_DOWN);
                    tranFy = bigDecimal1.floatValue();//(canvasHeight - videoHeight * (canW_d_vidW)) / 2.0F;
                } else {
                    ratio = canH_d_vidH;
                    ratioX = false;
                    ;
                    scaleFx = canH_d_vidH;
                    scaleFy = canH_d_vidH;
                    bigDecimal = new BigDecimal(canH_d_vidH).multiply(new BigDecimal(videoWidth));
                    bigDecimal1 = new BigDecimal(canvasWidth).subtract(bigDecimal).divide(BigDecimal.valueOf(2.0F), 2,RoundingMode.HALF_DOWN);

                    tranFx = bigDecimal1.floatValue();//(canvasWidth - videoWidth * (canH_d_vidH)) / 2.0F;
                }
                break;
            case ZOOM_END:
                if (videoRatio > canvasRatio) {
                    ratio = canW_d_vidW;
                    ratioX = true;
                    scaleFx = canW_d_vidW;
                    scaleFy = canW_d_vidW;
                     bigDecimal = new BigDecimal(canW_d_vidW).multiply(new BigDecimal(videoHeight));
                     bigDecimal1 = new BigDecimal(canvasHeight).subtract(bigDecimal);
                    tranFy = bigDecimal1.floatValue();//canvasHeight - videoHeight * (canW_d_vidW);
                } else {
                    ratio = canH_d_vidH;
                    ratioX = false;
                    scaleFx = canH_d_vidH;
                    scaleFy = canH_d_vidH;
                    bigDecimal = new BigDecimal(canH_d_vidH).multiply(new BigDecimal(videoWidth));
                    bigDecimal1 = new BigDecimal(canvasWidth).subtract(bigDecimal);
                    tranFx = bigDecimal1.floatValue();//canvasWidth - videoWidth * (canH_d_vidH);
                }
                break;
            case CLIP_CENTER:
                if (videoRatio > canvasRatio) {
                    ratio = canH_d_vidH;
                    ratioX = false;
                    scaleFx = canH_d_vidH;
                    scaleFy = canH_d_vidH;
                     bigDecimal = new BigDecimal(videoWidth).multiply(new BigDecimal(canH_d_vidH));
                     bigDecimal1 = new BigDecimal(canvasWidth).subtract(bigDecimal).divide(BigDecimal.valueOf(2.0F), 2,RoundingMode.HALF_DOWN);
                    tranFx = bigDecimal1.floatValue();//(canvasWidth - videoWidth * (canH_d_vidH)) / 2.0f;
                } else {
                    ratio = canW_d_vidW;
                    ratioX = true;
                    scaleFx = canW_d_vidW;
                    scaleFy = canW_d_vidW;
                     bigDecimal = new BigDecimal(videoHeight).multiply(new BigDecimal(canW_d_vidW));
                     bigDecimal1 = new BigDecimal(canvasHeight).subtract(bigDecimal).divide(BigDecimal.valueOf(2.0F), 2,RoundingMode.HALF_DOWN);
                    tranFy = bigDecimal1.floatValue();//(canvasHeight - videoHeight * (canW_d_vidW)) / 2.0f;
                }
                break;
            case INSIDE:
                if (videoWidth < canvasWidth && videoHeight < canvasHeight) {
                    tranFx = canW_vidW_f;
                    tranFy = canH_vidH_f;
                } else {
                    if (videoRatio > canvasRatio) {
                        ratio = canW_d_vidW;
                        ratioX = true;
                        scaleFx = canW_d_vidW;
                        scaleFy = canW_d_vidW;
                        bigDecimal = new BigDecimal(videoHeight).multiply(new BigDecimal(canW_d_vidW));
                        bigDecimal1 = new BigDecimal(canvasHeight).subtract(bigDecimal).divide(BigDecimal.valueOf(2.0F), 2,RoundingMode.HALF_DOWN);

                        tranFy = bigDecimal1.floatValue();//(canvasHeight - videoHeight * (canW_d_vidW)) / 2.0f;

                    } else {
                        ratio = canH_d_vidH;
                        ratioX = false;
                        scaleFx = canH_d_vidH;
                        scaleFy = canH_d_vidH;
                        bigDecimal = new BigDecimal(videoWidth).multiply(new BigDecimal(canH_d_vidH));
                        bigDecimal1 = new BigDecimal(canvasWidth).subtract(bigDecimal).divide(BigDecimal.valueOf(2.0F), 2,RoundingMode.HALF_DOWN);

                        tranFx = bigDecimal1.floatValue();//(canvasWidth - videoWidth * (canH_d_vidH)) / 2.0f;
                    }
                }
                break;
            default:
                ratio = canW_d_vidW;
                ratioX = true;
                scaleFx = canW_d_vidW;
                scaleFy = canW_d_vidW;
                break;
        }
    }
}
